package cn.lhz.indiaStation.analysis

import cn.lhz.util.spark.SparkUtil
import org.apache.spark.sql.{SaveMode, SparkSession}

/**
 * @author 年阔正
 * @version 1.0.0
 */

object fx01 {
  def main(args: Array[String]): Unit = {
    // 获取 SparkSession 对象
    val sparkSession: SparkSession = SparkUtil()

    // 读取hive表数据
    import sparkSession.sql
    // 获取国内空气污染数据
    val df = sql(
      """
        |select country,city,aqi_value
        | from air.dwd_global where country = 'China'
        |""".stripMargin)

    // 将 dataFrame 转为临时表
    val china_view: Unit = df.createOrReplaceTempView("china_view")

    // 每个城市空气质量
    val dws_china = sql("select city,count(*) as value from china_view group by city order by value desc")
    // 将分析结果保存 hive 数仓
    dws_china.write.mode(SaveMode.Overwrite).saveAsTable("air.dws_china")

    // 释放资源
    sparkSession.stop()
  }

}
